import { defineStore } from 'pinia'
import { ref } from 'vue'
export const useCartStore = defineStore(
  'user_cart',
  () => {
    // 购物车列表
    const showMsg = ref('')
    const carList = ref([])
    const addCar = (dessert) => {
      const index = carList.value.findIndex(
        (item) => item.dessertId === dessert.dessertId
      )
      // 存在则删除
      if (index > -1) {
        carList.value.splice(index, 1)
        showMsg.value = '该甜品已存在购物车'
      } else {
        showMsg.value = ''
      }
      carList.value.push(dessert)
    }
    const setCar = (value) => {
      carList.value = value
    }

    // 购物车商品数量
    const goodsNum = ref(0)
    // 加入购物车，商品数量加1
    const setGoodsNum = (value) => {
      goodsNum.value = value
    }

    return {
      goodsNum,
      setGoodsNum,
      carList,
      addCar,
      setCar,
      showMsg,
    }
  },
  { persist: true }
)
